package com.smtech.xz.oa;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

/**
 * 创建者： zhuhuanhuan
 * 创建时间： 2018/9/21-下午1:29
 * 说明：定位帮助类
 * 修改记录：
 */
final class LocationHelper {
    //经纬度
    private static String mLocation = "init";
    //声明AMapLocationClient类对象
    private static AMapLocationClient mLocationClient = null;
    //声明定位回调监听器
    private static AMapLocationListener mLocationListener = null;
    //声明AMapLocationClientOption对象
    private static AMapLocationClientOption mLocationOption = null;

    public static void initGaoDe(Context application) {
        initListener();

        //初始化定位
        mLocationClient = new AMapLocationClient(application);
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
//        mLocationClient.setApiKey("9412a4171d0b1584f9298d9b77fc5351");

        initOption();

        mLocationClient.setLocationOption(mLocationOption);
        //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
        mLocationClient.startLocation();
    }

    private static void initListener() {
        mLocationListener = new AMapLocationListener() {

            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if (aMapLocation != null) {
                    if (aMapLocation.getErrorCode() == 0) {
                        //可在其中解析amapLocation获取相应经度，纬度
                        mLocation = aMapLocation.getLongitude() + "," + aMapLocation.getLatitude();
                    } else {

                        //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                        String errorData = "location Error, ErrCode:"
                                + aMapLocation.getErrorCode() + ", errInfo:"
                                + aMapLocation.getErrorInfo();
//                        MSTMonitor.error(errorData, 20902, "watch", "LocationHelper");

                    }
                }
            }
        };
    }

    private static void initOption() {
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();

        /**
         * 设置定位场景，目前支持三种场景（签到、出行、运动，默认无场景）
         */
        mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);

        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //不允许模拟定位
        mLocationOption.setMockEnable(false);
        //关闭缓存机制
        mLocationOption.setLocationCacheEnable(false);
    }

    public static String getLocation() {
        return mLocation;
    }

    public static AMapLocationClient getLocationClient() {
        return mLocationClient;
    }
}
